//
//  CRECOMOrderProjectSiteController.m
//  XTCloud
//
//  Created by QA on 2017/10/25.
//  Copyright © 2017年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "CRECOMOrderProjectSiteController.h"
#import "Node.h"
#import "TreeTableView.h"

#import "WebServiceCommonRequest.h"
#import "NSDictionary+DataFormat.h"

@interface CRECOMOrderProjectSiteController ()<TreeTableCellDelegate>

@end

@implementation CRECOMOrderProjectSiteController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setTitle: @"选择工程部位"];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self getNetWorkExternalData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 网络获取数据

- (void)getNetWorkExternalData
{
    //@审核状态（0未审核，1已审核, 2已拒绝，4待接单）
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeGroup];
    [content setModuleType:@"CREC_DDGL_DD_XZDD_GCBW"];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        //添加数据，刷新页面
        NSArray *firstArray = nil;
        if(result.resultArray && result.resultArray.count>0){
            firstArray = [result.resultArray firstObject];
        }
        
        //显示数据处理
        if(firstArray && firstArray.count>0){
            [self handleData:firstArray];
        } else {
            //[self.tableView setTableViewNotDataDrawText:@"暂无浇筑令" isShow:YES];
        }
        
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
    }];
}

- (void)handleData:(NSArray *)array
{
    NSMutableArray *noteArray = [NSMutableArray array];
    for(int i=0;i<array.count;i++){
        NSDictionary *itemData = [array objectAtIndex:i];
        NSInteger ids = [itemData objectFormatIntegerForKey:@"ID"];
        NSInteger pid = [itemData objectFormatIntegerForKey:@"U_Pid"];
        NSString *name = [itemData objectFormatStringForKey:@"U_NAME"];
        
        //根目录数据处理
        if(pid == 0){
            Node *node = [[Node alloc] initWithParentId:pid nodeId:ids name:name depth:0 expand:YES];
            [noteArray addObject:node];
            [self handleChildData:array handleData:noteArray noteId:ids depth:0];
        }
    }
    
    CGRect frame = CGRectMake(0, 0, SuperWidth, SuperHeight);
    TreeTableView *tableview = [[TreeTableView alloc] initWithFrame:frame withData:noteArray];
    tableview.treeTableCellDelegate = self;
    [self.view addSubview:tableview];
    [tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.top.mas_equalTo(self.view.mas_top);
        make.bottom.mas_equalTo(self.view.mas_bottom);
    }];
}

- (void)handleChildData:(NSArray*)array handleData:(NSMutableArray*)noteArray noteId:(NSInteger)noteId depth:(NSInteger)depth
{
    for(int i=0;i<array.count;i++){
        NSDictionary *itemData = [array objectAtIndex:i];
        NSInteger ids = [itemData objectFormatIntegerForKey:@"ID"];
        NSInteger pid = [itemData objectFormatIntegerForKey:@"U_Pid"];
        NSString *name = [itemData objectFormatStringForKey:@"U_NAME"];
        
        //根目录数据处理
        if(pid == noteId){
            Node *node = [[Node alloc] initWithParentId:pid nodeId:ids name:name depth:(depth+1) expand:NO];
            [noteArray addObject:node];
            [self handleChildData:array handleData:noteArray noteId:ids depth:(depth+1)];
        }
    }
}

#pragma mark - TreeTableCellDelegate
- (void)cellClick:(Node *)node
{
    NSLog(@"%@",node.name);
}

-(void)cellClickEnd:(Node *)node
{
    NSLog(@"%@",node.name);
    NSArray *array = @[@(node.nodeId), node.name];
    if (self.backResultDelagate && [self.backResultDelagate respondsToSelector:@selector(backResultDataKey:dataValue:)]) {
        [self.backResultDelagate backResultDataKey:self.key dataValue:array];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

@end
